/**************************************************************************** 
* 
* Copyright (c) 2023  C*Core -   All Rights Reserved  
* 
* THIS SOFTWARE IS DISTRIBUTED "AS IS," AND ALL WARRANTIES ARE DISCLAIMED, 
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 
* PROJECT     : CCFC2011BC           
* DESCRIPTION : pwm demo code 
* HISTORY     : Initial version.
* @file     pwm_demo.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/
#include "pwm_demo.h"
#include "led_hw.h"
#include "console.h"
#include "intc_lld.h"

static PWM_Config_OPWFMB config_opwfmb1 = {
    PWM_INTERNAL_COUNTER,
    PWM_UC_PRESCALE_DIV1,
    START_RISING_EDGE,
    10,
    7,
};

static PWM_Config_MCB config_mcb1 = {
    PWM_INTERNAL_COUNTER,
    PWM_UC_PRESCALE_DIV1,
    MCB_COUNTER_UP_MODE,
    100,
    10
};

static PWM_Config_MCB config_mcb2 = {
    PWM_INTERNAL_COUNTER,
    PWM_UC_PRESCALE_DIV1,
    MCB_COUNTER_UP_DOWN_MODE,
    100,
    200
};

static PWM_Config_OPWMB config_opwmb1 = {
    PWM_COUNTER_BUS_A,
    PWM_UC_PRESCALE_DIV1,
    START_RISING_EDGE,
    10,
    50
};

static PWM_Config_OPWMCB config_opwmcb1 = {
    PWM_COUNTER_BUS_A,
    PWM_UC_PRESCALE_DIV1,
    START_RISING_EDGE,
    PWM_LEADING_EDGE,
    10,
    60
};

/**
 * @brief   Structure defining the ICU configuration "icucfg".
 */
static PWMConfig_t pwm_config_1 = {
    PWM_OPWFMB_MODE,
    &config_opwfmb1,
    NULL,
    NULL,
    NULL
};

/**
 * @brief   Structure defining the ICU configuration "icucfg".
 */
static PWMConfig_t pwm_config_2 = {
    PWM_MCB_MODE,
    NULL,
    &config_mcb1,
    NULL,
    NULL
};

/**
 * @brief   Structure defining the ICU configuration "icucfg".
 */
static PWMConfig_t pwm_config_3 = {
    PWM_OPWMB_MODE,
    NULL,
    NULL,
    &config_opwmb1,
    NULL
};

/**
 * @brief   Structure defining the ICU configuration "icucfg".
 */
static PWMConfig_t pwm_config_4 = { /* PRQA S 3218 */
    PWM_OPWMCB_MODE,
    NULL,
    NULL,
    NULL,
    &config_opwmcb1
};

static PWMConfig_t pwm_config_5 = {
    PWM_MCB_MODE,
    NULL,
    &config_mcb2,
    NULL,
    NULL
};

/*******************************************************************************
 * @brief      PWM_Test_Case1
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case1(void) /* PRQA S 3219 */
{
    uint8_t i = 0;

    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_0, 64);
    for (i = 0U; i < 9U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_1);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PWM_LLD_UC_Channel_Init(&PWMD_0, 16, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 16);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 24, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 24);

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_0_INTERNAL_COUNTER_OPWFMB_MODE \n");
    PSPRINTF("\n TEST EMIOS0 - CH[0]-CH[8] ,CH[16],CH[23],CH[24] !\n");
    PSPRINTF("\n GPIO        - PA[0]-PA[8] ,PE[0], PE[7], PE[11] !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_Test_Case2
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case2(void) /* PRQA S 3219 */
{
    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_1, 64);

    PWM_LLD_UC_Channel_Init(&PWMD_0, 0, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 0);

    PWM_LLD_UC_Channel_Init(&PWMD_0, 8, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 8);

    PWM_LLD_UC_Channel_Init(&PWMD_0, 16, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 16);

    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    PWM_LLD_UC_Channel_Init(&PWMD_0, 24, &pwm_config_1);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 24);

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_1_INTERNAL_COUNTER_OPWFMB_MODE \n");
    PSPRINTF("\n TEST EMIOS1 - CH[0], CH[8] ,CH[16],CH[23],CH[24] !\n");
    PSPRINTF("\n GPIO        - PG[14],PH[6] ,PG[7], PG[0], PG[1] !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_Test_Case3
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case3(void) /* PRQA S 3219 */
{
    uint8_t i = 0;
    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_0, 64);

    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_2);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    for (i = 0U; i < 23U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    for (i = 24U; i < 32U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_0_INTERNAL_COUNTER_MCB_OPWMB_MODE_1 \n");
    PSPRINTF("\n TEST EMIOS0 - CH[0]-CH[22], CH[24]-CH[31]  !\n");
    PSPRINTF("\n GPIO        - PA[0]-PA[8] ............ !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_MCB_ICU_USE
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void PWM_MCB_ICU_USE(void)
{
    PWM_LLD_Init(&PWMD_0, &EMIOS_0, 64);
    pwm_config_2.config_mcb->up_mode_peroid_value = 1000;
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_2);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);
}

/*******************************************************************************
 * @brief      PWM_Test_Case4
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case4(void) /* PRQA S 3219 */
{
    uint8_t i = 0;

    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_0, 64);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_5);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    for (i = 0U; i < 23U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    for (i = 24U; i < 32U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_0_INTERNAL_COUNTER_MCB_OPWMB_MODE_2 \n");
    PSPRINTF("\n TEST EMIOS0 - CH[0]-CH[22], CH[24]-CH[31]  !\n");
    PSPRINTF("\n GPIO        - PA[0]-PA[8] ............ !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_Test_Case5
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case5(void) /* PRQA S 3219 */
{
    uint8_t i = 0;

    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_1, 64);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_2);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    for (i = 0U; i < 23U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    for (i = 24U; i < 32U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_1_INTERNAL_COUNTER_MCB_OPWMB_MODE_1 \n");
    PSPRINTF("\n TEST EMIOS1 - CH[0]-CH[22], CH[24]-CH[31]  !\n");
    PSPRINTF("\n GPIO        - PH[0]-PH[8] ............ !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_Test_Case6
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case6(void) /* PRQA S 3219 */
{
    uint8_t i = 0;

    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_1, 64);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_5);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    for (i = 0U; i < 23U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    for (i = 24U; i < 32U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_1_INTERNAL_COUNTER_MCB_OPWMB_MODE_2 \n");
    PSPRINTF("\n TEST EMIOS1 - CH[0]-CH[22], CH[24]-CH[31]  !\n");
    PSPRINTF("\n GPIO        - PH[0]-PH[8] ............ !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_Test_Case7
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case7(void) /* PRQA S 3219 */
{
    uint8_t i = 0;
    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_0, 64);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_5);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    for (i = 1U; i < 8U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_4);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_0_INTERNAL_COUNTER_MCB_OPWMCB_MODE \n");
    PSPRINTF("\n TEST EMIOS0 - CH[1]-CH[7]!\n");
    PSPRINTF("\n GPIO        - PA[1]-PA[7]!\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_Test_Case8
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void PWM_Test_Case8(void) /* PRQA S 3219 */
{
    uint8_t i = 0;
    /*
    * Low level PWM driver initialization.
    */
    PWM_LLD_Init(&PWMD_0, &EMIOS_0, 64);
    PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config_2);
    PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);

    for (i = 0U; i < 23U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_Interrupt_Register(&PWMD_0, i);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    for (i = 24U; i < 32U; i++)
    {
        PWM_LLD_UC_Channel_Init(&PWMD_0, i, &pwm_config_3);
        PWM_LLD_UC_Channel_Enable(&PWMD_0, i);
    }

    PSPRINTF("========================================================\r\n");
    PSPRINTF("\n %s\n", __func__);
    PSPRINTF("\n PWM_Test_EMIOS_0_INTERNAL_COUNTER_MCB_OPWMB_MODE_1 \n");
    PSPRINTF("\n TEST EMIOS0 - CH[0]-CH[22], CH[24]-CH[31]  !\n");
    PSPRINTF("\n GPIO        - PA[0]-PA[8] ............ !\n");
    PSPRINTF("========================================================\r\n");
}

/*******************************************************************************
 * @brief      PWM_TEST_Demo
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void PWM_TEST_Demo(void)
{
    /* Set the current priority */
    INTC_CPR_Init((uint8_t)CPR_PRI_0);
    /* Function EMIOS0 INTERNAL COUNTER OPWFMB MODE Test */
#if PWM_CASE1
    PWM_Test_Case1();
#endif
    /* Function EMIOS1 INTERNAL COUNTER OPWFMB MODE Test */
#if PWM_CASE2
    PWM_Test_Case2();
#endif
    
    /* Function EMIOS0 INTERNAL COUNTER MCB OPWMB MODE1 Test */
#if PWM_CASE3
    PWM_Test_Case3();
#endif

    /* Function EMIOS0 INTERNAL COUNTER MCB OPWMB MODE2 Test */
#if PWM_CASE4
    PWM_Test_Case4();
#endif

    /* Function EMIOS1 INTERNAL COUNTER MCB OPWMB MODE1 Test */
#if PWM_CASE5
    PWM_Test_Case5();
#endif

    /* Function: EMIOS1 INTERNAL COUNTER MCB OPWMB MODE2 Test */
#if PWM_CASE6
    PWM_Test_Case6();
#endif

    /* Function: EMIOS0 INTERNAL COUNTER MCB OPWMCB MODE Test*/
#if PWM_CASE7
    PWM_Test_Case7();
#endif
     
    /* Function: EMIOS0 INTERNAL COUNTER MCB OPWMB MODE1 Test */
#if PWM_CASE8
    PWM_Test_Case8();
#endif
}

